//
//  SSChooseView.swift
//  weibo
//
//  Created by 邵帅 on 16/7/1.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

let HMChooseViewItemMargin: CGFloat = 5
let HMChooseViewItemWH: CGFloat = (UIScreen.mainScreen().bounds.size.width - 2 * HomeCellMargin - 2 * HMChooseViewItemMargin) / 3

let HMChooseViewReuseIdentifier = "HMChooseViewReuseIdentifier"
class SSChooseView: UICollectionView {

    var closure: (()->())?
    
    // 定义一个数组，保存图片
    private lazy var pictures = [UIImage]()
    
    let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 提供一个放法，从外面拿到图片
    func addImage(image: UIImage) {
        
        pictures.append(image)
        reloadData()
    }
    
    // 判断有没有超过九张
    func isMoreThanNine() -> Bool{
        
        
        return pictures.count >= 9 ? true : false
        
    }
    
    
    func getImage() -> [UIImage]{
        
        return pictures
    }
    
    func setupUI() {
        
        backgroundColor = UIColor.blueColor()
        flowLayout.itemSize = CGSizeMake(HMChooseViewItemWH, HMChooseViewItemWH)
        flowLayout.minimumLineSpacing = HMChooseViewItemMargin
        flowLayout.minimumInteritemSpacing = HMChooseViewItemMargin
        
        // 数据源，代理
        dataSource = self
        delegate = self
        
        // 注册cell
        
        registerClass(SSChooseViewCell.self, forCellWithReuseIdentifier: HMChooseViewReuseIdentifier)
    }
    
}

extension SSChooseView: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if pictures.count == 0 {
            return 0
        }else if pictures.count == 9 {
            return 9
        }else {
        
        return pictures.count + 1
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMChooseViewReuseIdentifier, forIndexPath: indexPath)as! SSChooseViewCell
        
        cell.backgroundColor = UIColor.brownColor()
        
        if indexPath.item == pictures.count {
            
            cell.image = UIImage(named: "compose_pic_add")
            cell.deleteBtn.hidden = true
        }else {
            
            cell.image = pictures[indexPath.item]
            cell.deleteBtn.hidden = false
        }
        
        
        
        cell.indexPath = indexPath
        
        cell.cellClosure = {(index)->() in
            
            self.pictures.removeAtIndex(index.item)
            
            self.reloadData()
        }
        
        return cell
    }
    
    // cell的选中
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.item == pictures.count {
            
            closure?()
        }        
    }
}

// 自定义cell
class SSChooseViewCell: UICollectionViewCell {
    
    var indexPath: NSIndexPath?
    
    var cellClosure: ((indexPath: NSIndexPath)->())?
    
    
    
    var image: UIImage? {
        
        didSet {
            
            iconImgView.image = image
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUIs()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUIs() {
        
        contentView.addSubview(iconImgView)
        contentView.addSubview(deleteBtn)
        
        iconImgView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView.snp_edges)
        }
        
        deleteBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(contentView.snp_right).offset(-HMChooseViewItemMargin)
            make.top.equalTo(contentView.snp_top).offset(HMChooseViewItemMargin)
        }
    }
    
    // 删除按钮的点击
    @objc private func deleteClick(btn: UIButton){
        
        cellClosure?(indexPath: self.indexPath!)
        
    }
    
    // 懒加载
    private lazy var iconImgView: UIImageView = UIImageView()
    
    private lazy var deleteBtn: UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        btn.sizeToFit()

        // 点击事件
        btn.addTarget(self, action: #selector(SSChooseViewCell.deleteClick(_:)), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    

}


